package com.cskaoyan.search.service.impl;

import com.cskaoyan.mall.dto.AllItemServiceResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.search.converter.ProductConverter;
import com.cskaoyan.search.entity.ItemDocument;
import com.cskaoyan.search.remote.ProductApiClient;
import com.cskaoyan.search.repository.ProductRepository;
import com.cskaoyan.search.service.InitDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.27 19:09
 * @description:
 */
@Service
@Slf4j
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductApiClient productApiClient;

    @Autowired
    ProductRepository productRepository;
    @Autowired
    ProductConverter productConverter;

    @Override
    public void initItems() {
        //TODO 把shopping中的item所有的数据获取到，通过服务的调用
        AllItemServiceResponse allProductItem = productApiClient.getAllProductItem();
        List<ItemDto> itemDtos = allProductItem.getCallData();

        //TODO 把数据插入 itemDto-->ItemDocument
        List<ItemDocument> itemDocuments = productConverter.items2Documnets(itemDtos);
        if (!CollectionUtils.isEmpty(itemDocuments)) {
            productRepository.saveAll(itemDocuments);
            log.info("elasticsearch 数据导入成功");
        }
        log.info("elasticsearch 数据导入失败");




    }
}
